Tailwind CSS

Grid


In web design, maintaining content organization is essential, and CSS grid layout offers an effective solution by structuring content into rows and columns. In this guide, we will explore how to leverage Tailwind CSS grid classes to construct web components efficiently. 


Additionally, we will learn the techniques for resizing rows and columns seamlessly, and ensuring responsiveness across various screen sizes.

 

Grid-Template-Columns:

The grid-template-columns property in CSS Grid Layout allows you to define the size and number of columns in a grid container. It specifies the width of each column individually or use various units and functions to create flexible layouts.

 

Values:

 

1. Fixed Size: We can define a fixed size for columns using pixels (px), ems (em), or rems (rem). For example, grid-template-columns: 200px 1fr 150px creates three columns: 200px wide, then one flexible column, and lastly a 150px column.
 

2. Percentage: Columns can also be sized as percentages relative to the total width of the grid container. This is useful for creating responsive layouts that adapt to different screen sizes. Example: grid-template-columns: 40% 60% creates two columns, one 40% wide and the other 60% wide.
 

3. fr Unit: The fr unit represents fractions of the remaining space after accounting for fixed-width and percentage-based columns. It allows for flexible distribution of space among columns. Example: grid-template-columns: 1fr 2fr creates two columns, with the second one taking up twice the space of the first.


4. Auto-Placement: Using the none value indicates that grid-template-columns will not define explicit grid tracks. Instead, columns are implicitly generated based on the grid-auto-columns property.

 

Example:

<div class="grid grid-cols-5 gap-4">
 <div class="bg-gray-200 p-4">Item 1</div>
 <div class="bg-gray-200 p-4 col-span-3">Item 2 (Span 3)</div>
 <div class="bg-gray-200 p-4">Item 3</div>
 <div class="bg-gray-200 p-4">Item 4</div>
 <div class="bg-gray-200 p-4">Item 5</div>
</div>

 

Another example:

<div class="grid grid-cols-200px 1fr">
 <div class="bg-gray-800 text-white p-4">Sidebar</div>
 <div class="bg-gray-100 p-4">Content Area</div>
</div>


Grid-Template-Row

It dictates the structure and sizing of rows within a grid container, specifying how many rows there will be and their individual heights.

 

Tailwind CSS allows for the rapid configuration of grid rows by accepting multiple values, mirroring the functionality of the CSS grid-template-rows property. This approach simplifies grid layout management and enhances efficiency, providing a convenient alternative to traditional CSS grid implementation for faster prototyping and styling.
We can specify the number of grid rows using the grid-rows-{n} utility class, where {n} is replaced with a number from 1 to 12.

 

Values:


1. Fixed Size: Specify a fixed height for a row using pixels (px), rems (rem), or other measurement units 

e.g.: grid-template-rows: 100px auto


2. Percentage: Define rows as a percentage of the available vertical space within the grid container 

e.g.: grid-template-rows: 30% 1fr
 

3. Fractions (fr): This unit allows for flexible row sizing relative to the available space. Multiple fr values will distribute the space proportionally 

e.g., grid-template-rows: 2fr 1fr (2:1 height ratio).
 

4. auto: This keyword instructs the browser to automatically distribute the remaining space equally among rows defined with auto.

 

Example:

<div class="grid grid-rows-3 gap-4">
 <div class="bg-blue-500">Row 1</div>
 <div class="bg-green-500">Row 2</div>
 <div class="bg-yellow-500">Row 3</div>
</div>

 

Another Example:

<div class="grid grid-rows-4 grid-cols-2 gap-4">
 <div class="bg-blue-500 h-16">Row 1</div>
 <div class="bg-green-500 h-32">Row 2</div>
 <div class="bg-yellow-500 h-24">Row 3</div>
 <div class="bg-red-500 h-12">Row 4</div>
</div>